package handle

import (
	"BaseSrv/common/global"
	"BaseSrv/model"
	"BaseSrv/proto"
	"context"
)

type UserServer struct{}

// 登录接口
func (s *UserServer) UserLogin(c context.Context, req *proto.LoginRequest) (*proto.UserLoginResponse, error) {
	var users model.User
	name := req.Name
	result := global.DB.Where("name = ?", name).First(&users)
	if result.Error != nil {
		return nil, result.Error
	}
	rsp := &proto.UserLoginResponse{
		Id:     int64(users.ID),
		Name:   users.Name,
		Mobile: users.Mobile,
	}
	return rsp, nil
}

// 用户列表接口
func (s *UserServer) UserList(c context.Context, req *proto.PageInfoRequest) (*proto.UserListResponse, error) {
	var user []model.User
	//定义返参结构体 -- 切片
	data := []*proto.UserInfoResponse{}
	result := global.DB.Find(&user)
	if result.Error != nil {
		return nil, result.Error
	}
	tatal := result.RowsAffected
	//把mysql查询出来的结构体 映射到定义的proto返回参数
	for _, value := range user {
		userData := proto.UserInfoResponse{
			Id:     int64(value.ID),
			Name:   value.Name,
			Mobile: value.Mobile,
		}
		data = append(data, &userData)
	}
	return &proto.UserListResponse{
		Data:  data,
		Total: tatal,
	}, nil
}

//func GormMake(user model.User, makeType int, tableName string, selectType int) model.User {
//	switch makeType { //操作类型
//	case 1:
//		//新增
//		global.DB.Table(tableName).Create(&user)
//	case 2:
//		//删除
//		global.DB.Table(tableName).Delete(&user)
//	case 3:
//		//修改
//		global.DB.Table(tableName).Save(&user)
//	case 4:
//		//查询
//		if selectType == 1 {
//			global.DB.Table(tableName).First(&user)
//		} else {
//			global.DB.Table(tableName).Find(&user)
//		}
//	}
//	return user
//}
